package com.inspur.his.controller;

import com.inspur.his.constant.Constant;
import com.inspur.his.pojo.Ro;
import com.inspur.his.pojo.RoExt;
import com.inspur.his.service.RoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class RoController {
    @Autowired
    private RoService roService;

    @RequestMapping("roListController")
    public String roList(HttpServletRequest req, Model md) {
        // 获取页面传递过来的查询条件
        String roid = req.getParameter("roid");
        String memberid = req.getParameter("memberid");
        String deptname = req.getParameter("deptname");
        String rotime = req.getParameter("rotime");

        HashMap map = new HashMap<String, String>();
        map.put("roid", roid);
        map.put("memberid", memberid);
        map.put("deptname", deptname);
        map.put("rotime", rotime);
        List<RoExt> roList = roService.roList(map);
        md.addAttribute("roList", roList);
        // 页面查询条件查询后锁定显示在页面上
        md.addAttribute("roid", roid);
        md.addAttribute("memberid", memberid);
        md.addAttribute("deptname", deptname);
        md.addAttribute("rotime", rotime);
        return "ro/roList.jsp";
    }

    @RequestMapping("roAddController")
    public String roAdd(Ro ro, Model model) throws Exception {
        Date day = new Date();
        SimpleDateFormat fm = new SimpleDateFormat("yyyyMMdd");
        String today = fm.format(day);
        ro.setOptime(day);
        roService.roAdd(ro);
        model.addAttribute("ro",ro);
        model.addAttribute("flag", Constant.USER_STATUS_ABLE);
        model.addAttribute("success","挂号保存成功！");
        return "ro/ghadd.jsp";
    }

    @RequestMapping("/deleteRoController")
    public  String deleteRo(String roid,Model model) {
        roService.deleteRoById(roid);
        return "redirect:roListController";
    }

    @RequestMapping("/checkRoid")
    @ResponseBody
    public Map checkRoid(String roid, Model model) {
        List<Ro> roList = roService.getRoByRoid(roid);
        Map map = new HashMap();
        map.put("roList", roList);
        if (roList.size() >= 1) {
            map.put("msg", "已存在该挂号单");
        }
        return map;
    }

    @RequestMapping("roList")
    @ResponseBody
    public List<RoExt> docInfo(HttpServletRequest req, Model md) {
        HashMap map = new HashMap<String, String>();
        String memberid =req.getParameter("memberid");
        map.put("memberid",memberid);
        List<RoExt> roList = roService.roList(map);
        return roList;
    }




    @RequestMapping("yyroAddController")
    public String yyroAdd(Ro ro, Model model) throws Exception {

        Date day = new Date();
        SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-DD");
        String today = fm.format(day);
        ro.setOptime(day);
        roService.roAdd(ro);
        model.addAttribute("ro",ro);
        model.addAttribute("flag", Constant.USER_STATUS_ABLE);
        model.addAttribute("success","挂号预约成功！");
        return "ro/yyghadd.jsp";
    }
}
